<template>
  <!-- 营销项目案例管理-案例征集查询 -->
  <div>
    <!-- 搜索 -->
    <TopFilter ref="conditionFilter" :list="topFilterInit" :cols="3" labelWidth="120" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :isSelectColumn="isSelectColumn"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
      columnsRef="projectOperating_case_collectInquire_index_01"
    >
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-download" :loading="exportLoading" @click="requestExportData">下载</el-button>
      </template>
    </FilterTable>

    <Drawer :visible.sync="visibleDrawer" :title="dialogTitleAdd" :width="920" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <info-modal :type="handleTypeAdd" :rowData="formData" @close="closeHandler" />
    </Drawer>
  </div>
</template>

<script>
import { getCaseCollectionList, getExcelList, selectListByBill } from '@/api/cms-api/projectOperating/case/oemCollect/index';
import { dictionary } from '@/utils/dictMixin';
import infoModal from '../oemCollect/info';
import { exportExcelFile } from '@/utils/index';
import { formatdateByDay } from '@/utils/index';
export default {
  mixins: [dictionary],
  components: {
    infoModal
  },
  //初始化数据  全局变量
  data() {
    return {
      topFilterInit: this.topFilterInitFun(),
      columns: this.creatHeader(),
      isSelectColumn: false,
      fetchApi: getCaseCollectionList, //ipa 接口
      fetchParams: {}, //参数
      visibleDrawer: false,
      dialogTitleAdd: '', //弹出标题
      handleTypeAdd: '',
      formData: {},
      exportLoading: false
    };
  },
  //方法、事件
  methods: {
    //导出
    async requestExportData() {
      let r = await this.$refs.conditionFilter.GET_FORM_DATA();
      let p = null;
      if (r != null && r.length > 1) {
        p = r[1];
      }
      let time = p.levyStartDate;
      let lssuedTime = p.lssuedStartDate;
      p = Object.assign({}, { timestart: time[0] }, p);
      p = Object.assign({}, { timeend: time[1] }, p);
      p = Object.assign({}, { lssuedStartTime: lssuedTime[0] }, p);
      p = Object.assign({}, { lssuedEndTime: lssuedTime[1] }, p);
      this.exportLoading = true;
      // this.$refs.conditionFilter.SUBMIT_FORM();
      getExcelList(p).then(res => {
        exportExcelFile(res.data, '案例征集主数据');
        this.exportLoading = false;
      });
    },
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: '优秀案例征集编号', fieldName: 'caseLevyCode', initialValue: '' },
        { type: 'INPUT', label: '优秀案例征集名称', fieldName: 'caseLevyName', initialValue: '' },
        {
          type: 'SELECT',
          label: '下发状态',
          fieldName: 'caseLevyStatus',
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDictList('2533')
        },
        {
          type: 'RANGE_DATE',
          label: '征集开始时间',
          fieldName: 'levyStartDate',
          initialValue: ''
        },
        {
          type: 'RANGE_DATE',
          label: '下发时间',
          fieldName: 'lssuedStartDate',
          initialValue: ''
        }
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.drawerHandle(props.row, 'show')}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '下发状态',
          dataIndex: 'caseLevyStatus',
          sorter: true,
          filterItems: this.createDictList('2533'),
          render: props => {
            //编码转汉字
            return <span>{this.createDictText(props.row.caseLevyStatus, '2533')}</span>;
          }
        },
        {
          title: '优秀案例征集编号',
          dataIndex: 'caseLevyCode',
          sorter: true
        },
        { title: '优秀案例征集名称', dataIndex: 'caseLevyName', sorter: true },
        {
          title: '征集开始时间',
          dataIndex: 'levyStartDate',
          sorter: true,
          render: props => {
            return <span>{formatdateByDay(props.row.levyStartDate)} </span>;
          }
        },
        {
          title: '征集结束时间',
          dataIndex: 'levyEndDate',
          sorter: true,
          render: props => {
            return <span>{formatdateByDay(props.row.levyEndDate)} </span>;
          }
        },
        {
          title: '项目开始时间',
          dataIndex: 'projectExecuteStartDate',
          sorter: true,
          render: props => {
            return <span>{formatdateByDay(props.row.projectExecuteStartDate)} </span>;
          }
        },
        {
          title: '项目结束时间',
          dataIndex: 'projectExecuteEndDate',
          sorter: true,
          render: props => {
            return <span>{formatdateByDay(props.row.projectExecuteEndDate)} </span>;
          }
        },
        {
          title: '下发时间',
          dataIndex: 'lssuedStartDate',
          sorter: true,
          render: props => {
            return <span>{formatdateByDay(props.row.lssuedStartDate)} </span>;
          }
        }
      ];
    },
    //明细
    async drawerHandle(row, type) {
      if (type === 'show') {
        let res = {};
        this.handleTypeAdd = 'show';
        this.dialogTitleAdd = `优秀案例征集明细`;
        this.formData = row;
        this.visibleDrawer = true;
      }
    },
    //搜索回调
    changeHandle(data) {
      let time = data.levyStartDate;
      let lssuedTime = data.lssuedStartDate;
      data = Object.assign({}, { timestart: time[0] }, data);
      data = Object.assign({}, { timeend: time[1] }, data);
      data = Object.assign({}, { lssuedStartTime: lssuedTime[0] }, data);
      data = Object.assign({}, { lssuedEndTime: lssuedTime[1] }, data);
      // console.log(data);
      this.fetchParams = Object.assign({}, data);
    },
    closeHandler(val, type) {
      this.visibleDrawer = false;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    formatDates: function(value) {
      // 这里的 value 就是需要过滤的数据
      if (value == null || value == '') {
        return '';
      }
      var date = new Date(value);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      return year + '-' + month + '-' + day;
    }
  }
};
</script>

<style scoped></style>
